/*************************************************************
 * Title: UserServiceImpl.java
 * Description: 
 * Author: Huang Shaobin
 * Email: shaobin.software@gmail.com
 * CreateTime: Feb 22, 2013 4:52:18 PM
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.biz.system.service.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;

import snow.biz.basic.service.impl.HibernateBaseServiceImpl;
import snow.biz.system.model.User;
import snow.biz.system.service.IUserService;
import snow.core.domain.Pagination;
import snow.core.enums.Status;
import snow.core.exceptions.BusinessException;
import snow.core.persistence.AndedConditions;
import snow.core.persistence.BinaryOperator;
import snow.core.persistence.Condition;
import snow.core.persistence.Conditions;
import snow.core.persistence.EntityHelper;
import snow.core.persistence.Order;
import snow.core.security.Principal;
import snow.core.toolkit.Validator;

/**
 *
 */
public class UserServiceImpl extends HibernateBaseServiceImpl implements IUserService {

	/**
	 * 创建用户
	 * 
	 * @param user
	 * @return
	 * @throws BusinessException
	 * @see snow.biz.system.service.IUserService#createUserFrom(snow.biz.system.model.User)
	 */
	public Integer createUserFrom(User user) throws BusinessException {
		final Conditions conditions = new AndedConditions();
		conditions.addCondition(new Condition("loginName", BinaryOperator.EQ, user.getLoginName()));
		// conditions.addCondition(new Condition("status", BinaryOperator.EQ, Status.SUCCESS));
		final List<Order> orders = Collections.emptyList();
		final List<User> userList = this.searchUsersFrom(conditions, orders, 0, 1);
		Validator.isEmpty(userList, "200109", "failure.system.user.exist");
		return super.save(user, Integer.class);
	}

	/**
	 * 物理删除用户
	 * 
	 * @param user
	 * @throws BusinessException
	 * @see snow.biz.system.service.IUserService#deleteUserFrom(snow.biz.system.model.User)
	 */
	public void deleteUserFrom(User user) throws BusinessException {
		User targetUser = user == null ? null : (user.getId() == null ? null : searchUserFrom(user.getId()));
		Validator.notNull(targetUser, "200110", "failure.system.user.not.found");
		super.delete(targetUser);
	}

	/**
	 * 修改用户信息
	 * 
	 * @param user
	 * @throws BusinessException
	 * @see snow.biz.system.service.IUserService#modifyUserFrom(snow.biz.system.model.User)
	 */
	public void modifyUserFrom(User user) throws BusinessException {
		User targetUser = user == null ? null : (user.getId() == null ? null : searchUserFrom(user.getId()));
		Validator.isTrue(EntityHelper.validate(targetUser), "200110", "failure.system.user.not.found");
		try {
			BeanUtils.copyProperties(user, targetUser);
		} catch (BeansException e) {
			log.warn(e.getMessage(), e);
		}
		super.update(targetUser);
	}

	/**
	 * 逻辑删除
	 * 
	 * @param user
	 * @throws BusinessException
	 * @see snow.biz.system.service.IUserService#removeUserFrom(snow.biz.system.model.User)
	 */
	public void removeUserFrom(User user) throws BusinessException {
		User targetUser = user == null ? null : (user.getId() == null ? null : searchUserFrom(user.getId()));
		Validator.isTrue(EntityHelper.validate(targetUser), "200110", "failure.system.user.not.found");
		targetUser.setStatus(Status.FAILURE);
		super.update(targetUser);
	}

	/**
	 * @param id
	 * @return
	 * @see snow.core.security.PrincipalService#searchPrincipalFrom(java.io.Serializable)
	 */
	public Principal searchPrincipalFrom(Serializable id) {
		try {
			return id instanceof Integer ? searchUserFrom((Integer) id) : null;
		} catch (BusinessException e) {
			return null;
		}
	}

	public User searchUserFrom(Integer id) throws BusinessException {
		return id == null ? null : super.get(User.class, id);
	}

	public Pagination searchUsersForPagingFrom(Conditions conditions, List<Order> orders, int start, int limit) throws BusinessException {
		return super.findForPaging(User.class, conditions, orders, start, limit);
	}

	public List<User> searchUsersFrom(Conditions conditions, List<Order> orders, int start, int limit) throws BusinessException {
		return super.find(User.class, conditions, orders, start, limit);
	}

}
